package com.ruoyi.common.runner;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

@Component
public class MyStartupRunner implements CommandLineRunner {
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public void run(String... args) throws Exception {
        //该方法为项目启动时，自动加载的
        //先获取省市区json文件中的全部数据，转为JSONArray
        //先转换为流
        InputStream inputStream = this.getClass().getResourceAsStream("/json/Region.json");
        //再转为JSONNode
        ObjectMapper objectMapper = new ObjectMapper();
        JsonNode jsonNode = objectMapper.readTree(inputStream);
        //再转为JSONArray,如果流文件中内容为空，则不操作
        if (jsonNode != null) {
            //先转为List数组 命名为regionList
            List<JSONObject> regionList = new ArrayList<>();
            JSONArray jsonArray = JSONArray.parseArray(jsonNode.toString());
            for (Object o : jsonArray) {
                JSONObject jsonObject = (JSONObject) o;
                regionList.add(jsonObject);
            }
            //将全部的code变为value，name变为label
            List<JSONObject> regionChangedList = new ArrayList<>();
            for (JSONObject item1 : regionList) {
                JSONObject json = new JSONObject();
                String code1 = item1.getString("code");
                String name1 = item1.getString("name");
                List<JSONObject> cityList = item1.getList("children", JSONObject.class);
                List<JSONObject> cityChangedList = new ArrayList<>();
                for (JSONObject item2 : cityList) {
                    JSONObject json2 = new JSONObject();
                    String code2 = item2.getString("code");
                    String name2 = item2.getString("name");
                    List<JSONObject> areaList = item2.getList("children", JSONObject.class);
                    List<JSONObject> areaChangedList = new ArrayList<>();
                    for (JSONObject item3 : areaList) {
                        JSONObject json3 = new JSONObject();
                        String code3 = item3.getString("code");
                        String name3 = item3.getString("name");
                        json3.put("value", code3);
                        json3.put("label", name3);
                        areaChangedList.add(json3);
                    }
                    json2.put("value", code2);
                    json2.put("label", name2);
                    json2.put("children", areaChangedList);
                    cityChangedList.add(json2);
                }
                json.put("value", code1);
                json.put("label", name1);
                json.put("children", cityChangedList);
                regionChangedList.add(json);
            }
            String jsonString = JSONObject.toJSONString(regionChangedList);
            //先将json直接存储进redis，方便天气组件使用联动组件调用
            redisTemplate.opsForValue().set("regionList", jsonString);
            //获取省数组
            List<JSONObject> provinceList = new ArrayList<>();
            for (JSONObject jsonObject : regionList) {
                JSONObject provinceJSON = new JSONObject();
                provinceJSON.put("value", jsonObject.get("code"));
                provinceJSON.put("label", jsonObject.get("name"));
                provinceList.add(provinceJSON);
            }
            //存储省数组
            redisTemplate.opsForHash().put("region", "province", provinceList);
            //存储省市对应关系的数组：code为省的code，children为只包含code和name的市的数组
            List<JSONObject> cityList = new ArrayList<>();
            List<JSONObject> cityReturnList = new ArrayList<>();

            for (JSONObject jsonObject : regionList) {
                List<JSONObject> cityFilteredList = new ArrayList<>();
                JSONObject cityJSON = new JSONObject();
                String provinceCode = jsonObject.getString("code");
                List<JSONObject> children = jsonObject.getList("children", JSONObject.class);
                cityList.addAll(children);
                for (JSONObject item : children) {
                    JSONObject json = new JSONObject();
                    json.put("value", item.get("code"));
                    json.put("label", item.get("name"));
                    cityFilteredList.add(json);
                }
                cityJSON.put("code", provinceCode);
                cityJSON.put("children", cityFilteredList);
                cityReturnList.add(cityJSON);
            }
            //存储市数组
            redisTemplate.opsForHash().put("region", "city", cityReturnList);

            //进行区数组过滤
            List<JSONObject> areaReturnList = new ArrayList<>();
            for (JSONObject jsonObject : cityList) {
                JSONObject json = new JSONObject();
                String cityCode = jsonObject.getString("code");
                List<JSONObject> children = jsonObject.getList("children", JSONObject.class);
                List<JSONObject> filteredChildren = new ArrayList<>();
                for (JSONObject item : children) {
                    JSONObject areaJSON = new JSONObject();
                    areaJSON.put("value", item.get("code"));
                    areaJSON.put("label", item.get("name"));
                    filteredChildren.add(areaJSON);
                }
                json.put("code", cityCode);
                json.put("children", filteredChildren);
                areaReturnList.add(json);
            }

            //存储区数组
            redisTemplate.opsForHash().put("region", "area", areaReturnList);
            JSONObject returnJSON = new JSONObject();
            returnJSON.put("省", provinceList);
            returnJSON.put("市", cityReturnList);
            returnJSON.put("区", areaReturnList);
            System.out.println("打印日志：" + returnJSON.toJSONString());
        }
    }
}